﻿using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace Npu_Platform
{
    class Client : Network
    {
        private Socket _connection;
        public Client(IPAddress ClientIpav6, int Port)
            : base(ClientIpav6, Port)
        {


        }
        public Boolean ClientInit(out String s, out Exception e)
        {
            s = "";
            e = new Exception();
            if (Ipv6Support() == false)
            {
                return false;
            }
            else
            {
                try
                {

                    _connection = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
                    _connection.Connect(_Ipep);
                    byte[] b = System.Text.ASCIIEncoding.ASCII.GetBytes("hello world");
                    for (int x = 0; x < 10; x++)
                    {
                        s = "TX: " + System.Text.ASCIIEncoding.ASCII.GetString(b);
                        _connection.Send(b);

                        Thread.Sleep(1000);
                    }

                    _connection.Close();
                    return true;
                }
                catch (System.Exception ex)
                {
                    e = ex;
                    return false;
                }

            }
        }
        public Boolean ClientInit(out String s)
        {
            s = "";

            if (Ipv6Support() == false)
            {
                return false;
            }
            else
            {

                _connection = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
                _connection.Connect(_Ipep);
                byte[] b = System.Text.ASCIIEncoding.ASCII.GetBytes("hello world");
                for (int x = 0; x < 10; x++)
                {
                    s = "TX: " + System.Text.ASCIIEncoding.ASCII.GetString(b);
                    _connection.Send(b);

                    Thread.Sleep(1000);
                }

                _connection.Close();
                return true;


            }
        }
        public Boolean ClientInit()
        {
            String s = "";

            if (Ipv6Support() == false)
            {
                return false;
            }
            else
            {
                try
                {
                    _connection = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
                    _connection.Connect(_Ipep);
                    byte[] b = System.Text.ASCIIEncoding.ASCII.GetBytes("hello world");
                    for (int x = 0; x < 10; x++)
                    {
                        s = "TX: " + System.Text.ASCIIEncoding.ASCII.GetString(b);

                        _connection.Send(b);
                        if (SendMessage != null)
                        {
                            SendMessage(s);
                        }

                        Thread.Sleep(1000);
                    }

                    _connection.Close();
                    return true;
                }
                catch (System.Exception ex)
                {
                    if (SendError!=null)
                    {
                        SendError(ex);
                    }
                    return false;
                }
                


            }
        }
        public delegate void SendMessageEventHandler(String s);
        public event SendMessageEventHandler SendMessage;
        public delegate void SendErrorEventHandler(Exception ex);
        public event SendErrorEventHandler SendError;
    }
}